package com.example.review;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Throughput
 * Package: com.example.review
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-05
 * Time: 12:16
 */


//生成者 - 提高吞吐量
public class Throughput {

    public static void main(String[] args) {

        Properties properties = new Properties();

        //配置必要参数
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        //提高吞吐量的一些配置
        //设置批次大小
        properties.put(ProducerConfig.BATCH_SIZE_CONFIG,16284);
        //设置缓冲区大小 32m 默认也是32m
        properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG,33554432);
        //设置linger.ms 等待时长 默认是0
        properties.put(ProducerConfig.LINGER_MS_CONFIG,1);
        //设置压缩方式
        properties.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,"snappy");

        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        kafkaProducer.send(new ProducerRecord<>("first","hello)))"));

        //关闭资源
        kafkaProducer.close();


    }

}
